import logging
import requests
import json
from typing import List

from mcp.server.fastmcp import FastMCP

import __init__

mcp = FastMCP("main")


@mcp.tool('main_relation_check')
async def relation_check(question: str, knowledge: List[str]):
    # 模型地址
    classification_url = "http://110.122.0.37:32305/text_relevancy_evaluator"
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    relation_result = []
    for document_text in knowledge:
        payload = {
            "question": question.encode('utf-8').decode('unicode_escape'),
            "document_text": document_text.encode('utf-8').decode('unicode_escape')
        }
        response = requests.post(classification_url, data=json.dumps(payload), headers=headers)
        relation_result.append(response.content.decode('utf-8'))
    return relation_result

# 该启动函数是必须的，不能删除
if __name__ == "__main__":
    # 日志打印是确保__init__引用
    logging.debug('', __init__)
    mcp.run(transport='stdio')
